package com.store.pet.petstore.util;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.store.pet.petstore.R;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public class BoundsLinearLayout extends LinearLayout {

    private TextView titleTv;
    private ImageView imgTv;
    public static int LEFT = 0;
    public static int TOP = 1;
    public static int RIGHT = 2;
    public static int BOTTOM = 3;
    private boolean leftView = false;
    private boolean topView = false;
    private View[] views = new View[5];

    public BoundsLinearLayout(Context context) {
        super(context);
    }

    public BoundsLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BoundsLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BoundsLinearLayout);
        float widthInteger = typedArray.getDimension(R.styleable.BoundsLinearLayout_srcWidth, 25);
        float heightInteger = typedArray.getDimension(R.styleable.BoundsLinearLayout_srcHeight, 25);
        float pdInteger = typedArray.getDimension(R.styleable.BoundsLinearLayout_srcPandding, 5);
        float sizeInteger = typedArray.getDimension(R.styleable.BoundsLinearLayout_srcTitleSize, 13);
        int colorInteger = typedArray.getColor(R.styleable.BoundsLinearLayout_srcTitleColor, 0x333333);
        int indexCount = typedArray.getIndexCount();
        int size = ConvertUtils.px2sp(sizeInteger);
        for (int i = 0; i < indexCount; i++) {
            imgTv = new ImageView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) widthInteger, (int) heightInteger);
            int typedArrayIndex = typedArray.getIndex(i);
            switch (typedArrayIndex) {
                case R.styleable.BoundsLinearLayout_srcLeft:
                    setOrientation(HORIZONTAL);
                    Drawable leftDrawable = typedArray.getDrawable(typedArrayIndex);
                    imgTv.setBackground(leftDrawable);
                    params.setMargins(0, 0, (int) pdInteger, 0);
                    imgTv.setLayoutParams(params);
                    views[0] = imgTv;
                    addView(imgTv, 0);
                    break;
                case R.styleable.BoundsLinearLayout_srcTop:
                    setOrientation(VERTICAL);
                    Drawable topDrawable = typedArray.getDrawable(typedArrayIndex);
                    imgTv.setBackground(topDrawable);
                    params.setMargins(0, 0, 0, (int) pdInteger);
                    imgTv.setLayoutParams(params);
                    views[1] = imgTv;
                    addView(imgTv, 0);
                    break;
                case R.styleable.BoundsLinearLayout_srcRight:
                    setOrientation(HORIZONTAL);
                    leftView = true;
                    Drawable rightDrawable = typedArray.getDrawable(typedArrayIndex);
                    imgTv.setBackground(rightDrawable);
                    params.setMargins((int) pdInteger, 0, 0, 0);
                    imgTv.setLayoutParams(params);
                    views[2] = imgTv;
                    addView(imgTv);
                    break;
                case R.styleable.BoundsLinearLayout_srcBottom:
                    setOrientation(VERTICAL);
                    topView = true;
                    Drawable bottomDrawable = typedArray.getDrawable(typedArrayIndex);
                    imgTv.setBackground(bottomDrawable);
                    params.setMargins(0, (int) pdInteger, 0, 0);
                    imgTv.setLayoutParams(params);
                    views[3] = imgTv;
                    addView(imgTv);
                    break;
                case R.styleable.BoundsLinearLayout_srcTitle:
                    titleTv = new TextView(context);
                    String title = typedArray.getString(typedArrayIndex);
                    titleTv.setText(title);
                    titleTv.setTextColor(colorInteger);
                    titleTv.setTextSize(size);
                    views[4] = titleTv;
                    if (!leftView && !topView) {
                        addView(titleTv);
                    } else {
                        addView(titleTv, 0);
                    }
                    break;
            }
        }
        typedArray.recycle();
    }

    public void setImageRes(int resId, int gravity) {
        ImageView imgView = (ImageView) views[gravity];
        if (imgView != null) {
            imgView.setBackgroundResource(resId);
        }
    }

    public void setTitleContent(String content) {
        TextView tv = (TextView) views[4];
        if (tv != null) {
            tv.setText(content);
        }
    }

    public String getTitle() {
        TextView tv = (TextView) views[4];
        if (tv != null) {
            return tv.getText().toString();
        }
        return null;
    }

    public void setTitleColor(int color) {
        TextView tv = (TextView) views[4];
        if (tv != null) {
            tv.setTextColor(color);
        }
    }

}
